// pages/profile-detail/profile-detail.js
Page({
  data: {
    profileData: null,
    profileId: null
  },

  onLoad: function (options) {
    const id = options.id;
    if (id) {
      this.setData({
        profileId: parseInt(id)
      });
      this.loadProfileData(parseInt(id));
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  onShow: function () {
    // 页面显示时刷新数据
    if (this.data.profileId) {
      this.loadProfileData(this.data.profileId);
    }
  },

  // 加载档案数据
  loadProfileData: function(id) {
    const profileList = wx.getStorageSync('profiles') || [];
    const profileData = profileList.find(item => item.id === id);
    
    if (profileData) {
      this.setData({
        profileData: profileData
      });
    } else {
      this.setData({
        profileData: null
      });
      wx.showToast({
        title: '档案不存在',
        icon: 'none'
      });
    }
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  },

  // 编辑档案
  editProfile: function() {
    if (!this.data.profileData) {
      wx.showToast({
        title: '档案不存在',
        icon: 'none'
      });
      return;
    }
    
    wx.navigateTo({
      url: `/pages/profile-edit/profile-edit?id=${this.data.profileId}`
    });
  }
});